package org.cxy.demo.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/users/{id}")
    public User getUser(@PathVariable String id) {
        // 模拟业务处理 - 这个方法会被监控
        try {
            Thread.sleep(100); // 模拟数据库查询等耗时操作
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return new User(id, "用户 " + id);
    }

    @PostMapping("/users")
    public User createUser(@RequestBody User user) {
        // 模拟创建用户 - 这个方法会被监控且会触发慢方法警告
        try {
            Thread.sleep(650); // 故意设置超过阈值500ms
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return user;
    }

    @GetMapping("/fast")
    public String fastMethod() {
        // 快速方法 - 不会触发慢方法警告
        return "快速响应";
    }

    @GetMapping("/error-test")
    public String errorMethod() {
        // 测试异常情况
        throw new RuntimeException("测试异常");
    }

    public static class User {
        private String id;
        private String name;

        public User() {}

        public User(String id, String name) {
            this.id = id;
            this.name = name;
        }

        // getters and setters
        public String getId() { return id; }
        public void setId(String id) { this.id = id; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }

        @Override
        public String toString() {
            return "User{id='" + id + "', name='" + name + "'}";
        }
    }
}